<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
	xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body>
	<div class="layui-fluid">
		<hr class="layui-bg-gray">
		<div class="layui-inline">
			<div class="layui-input-inline">
				<input class="layui-input" name="username" placeholder="请输入用户名" autocomplete="off" />
			</div>
			<div class="layui-input-inline">
				<input class="layui-input" name="operation" placeholder="请输入操作类型" autocomplete="off" />
			</div>
			<button class="layui-btn layui-btn-normal" id="search" data-type="reload">搜索</button>
		</div>
		<table id="log-table" lay-filter="log-table"></table>
	</div>
	<!-- 工具栏 -->
	<script type="text/html" id="toolbarUser">
        <div class="layui-btn-container" shiro:hasPermission="sys:log:batchRemove" >
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchRemove"><i class="layui-icon layui-icon-delete"></i>删除</button>
        </div>
    </script>
	<!-- 操作列 -->
	<script type="text/html" id="log-state">
     <div class="layui-btn-group" shiro:hasPermission="sys:log:remove" >
        <button class="layui-btn layui-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i></button>
    </div>
    </script>
	<div th:include="include::footer"></div>
	<script>
		layui.use([ 'table' ], function() {
			var $ = layui.jquery;
			var table = layui.table;
			// 渲染表格
			var renderTable = function() {
				layer.load(2);
				table.render({
					elem : '#log-table',
					url : '/sys/log/list',
					toolbar : '#toolbarUser',
					page : true,
					id : 'logReload',
					parseData : chaoLayuiTable.parseData,
					request : chaoLayuiTable.request,
					response : chaoLayuiTable.response,
					//limit : 1,
					cols : [ [ {
						type : 'checkbox',
						fixed : 'left'
					}, {
						field : 'id',
						title : 'ID',
						fixed : 'left',
						unresize : true,
						sort : true,
						width : 80
					}, {
						field : 'userId',
						title : 'user',
						fixed : 'left',
						unresize : true,
						sort : true,
						width : 80
					}, {
						field : 'username',
						align : 'center',
						title : '用户名',
					}, {
						field : 'operation',
						align : 'center',
						title : '操作'
					}, {
						align : 'center',
						field : 'time',
						title : '用时'
					}, {
						align : 'center',
						field : 'method',
						title : '方法',
					}, {
						align : 'center',
						field : 'params',
						title : '参数',
					}, {
						align : 'center',
						field : 'ip',
						title : 'IP地址'
					}, {
						align : 'center',
						field : 'gmtCreate',
						title : '创建时间'
					}, {
						title : '操作',
						toolbar : '#log-state',
						align : 'center'
					} ] ],
					done : function() {
						layer.closeAll('loading');
					}
				});

			};
			renderTable();
			// 搜索 
			var active = {
				reload : function() {
					//执行重载
					table.reload('logReload', {
						page : {
							curr : 1
						//重新从第 1 页开始
						},
						where : {
							username : $('[name=username]').val(),
							operation : $('[name=operation]').val(),
						}
					});
				}
			};

			$('#search').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});

			//头工具栏事件
			table.on('toolbar(log-table)', function(obj) {
				var checkStatus = table.checkStatus(obj.config.id);
				switch (obj.event) {
				case 'batchRemove'://批量删除
					var data = checkStatus.data;
					if (data.length < 1) {
						layer.msg('请至少选择一个');
						break;
					}
					//批量删除
					batchRemove(data);
				}
			});

			//监听工具条
			table.on('tool(log-table)', function(obj) {
				var data = obj.data;
				console.log(data)
				var layEvent = obj.event;
				if (layEvent === 'del') {//删除
					remove(data.id);
				}
			});

			function remove(id) {
				layer.confirm('确定要删除选中的记录？', {
					btn : [ '确定', '取消' ]
				}, function() {
					$.ajax({
						url : "/sys/log/remove",
						type : "post",
						data : {
							id : id
						},
						success : function(r) {
							if (r.retCode == '0000') {
								layer.msg("删除成功");
								renderTable();
							} else {
								layer.msg(r.retMsg);
							}
						}
					});
				})
			}

			function batchRemove(data) {
				layer.confirm("确认要删除选中的'" + data.length + "'条数据吗?", {
					btn : [ '确定', '取消' ]
				}, function() {
					var ids = new Array();
					$.each(data, function(i, row) {
						ids[i] = row['id'];
					});
					console.log(ids);
					$.ajax({
						type : 'POST',
						data : {
							"ids" : ids
						},
						url : '/sys/log/batchRemove',
						success : function(r) {
							if (r.retCode == '0000') {
								layer.msg(r.retMsg);
								renderTable();
							} else {
								layer.msg(r.retMsg);
							}
						}
					});
				});
			}

			// 
		});
	</script>
</body>
</html>